﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using SpLocalizer.Common;
using SpLocalizer.Common.DbApi;
using Core;
using SpLocalizer.Common.Extensions;
using Core.Threading.ThreadPools;

namespace SpLocalizer.TranslationQueue
{

   
    public class TranslateThreadPool
    {
        private readonly LogWriter _log;
        private readonly ExtendedThreadPool _extendedThreadPool;

        public TranslateThreadPool()
        {
            _log = ServiceContainer.Resolve<LogWriter>();
            _extendedThreadPool = ServiceContainer.Resolve<ExtendedThreadPool>();
        }


        public void Start()
        {
            BaseTranslationTask.ResetTranslationTasksQueue();

            //Populate queue with translation tasks 
            BaseTranslationTask toTranslate = BaseTranslationTask.LoadFromNextDatabaseTask();
            if (toTranslate == null)
                _log.Write("No task for translation");

            while (toTranslate != null)
            {

                TranslationTask action = new TranslationTask(toTranslate);
                 _extendedThreadPool.AddTask(action);
                 
                // Load next
                 toTranslate = BaseTranslationTask.LoadFromNextDatabaseTask();
            }


        }

        public bool ProcessSpecialKey(ConsoleKeyInfo keyInfo)
        {
            switch (keyInfo.Key)
            {
                case ConsoleKey.S:
                     _log.Write(_extendedThreadPool.GetStatistic().ToString("ThreadPool"),Constants.InfoLogCategory);                         
                    return true;
                case ConsoleKey.Enter:
                    return false;
            }

            return true;
        }
  
    }
}
